
package com.jf.cloud.user.controller.multishop;

import com.jf.cloud.api.user.vo.UserManagerVO;
import com.jf.cloud.api.user.vo.UserTagApiVO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.user.dto.ShopCustomerDTO;
import com.jf.cloud.user.dto.UserDTO;
import com.jf.cloud.user.dto.UserManagerDTO;
import com.jf.cloud.user.manager.UserExcelManager;
import com.jf.cloud.user.service.UserService;
import com.jf.cloud.user.service.UserTagUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


/**
 * 用户信息
 * @author zz
 */
@RestController("multishopUserController")
@RequestMapping("/m/user")
@Tag(name = "multishop-客户信息")
public class UserController {

    @Value("${jfcloud.expose.operation.auth:}")
    private Boolean permission;

    @Autowired
    private UserService userService;
    @Autowired
    private UserTagUserService userTagUserService;
    @Autowired
    private UserExcelManager userExcelManager;



    @GetMapping("/shop_customer_page")
    @Operation(summary = "获取客户列表", description = "分页获客户列表")
    public ServerResponseEntity<PageVO<UserManagerVO>> userPage(@Valid PageDTO pageDTO, @ParameterObject ShopCustomerDTO shopCustomerDTO) {
        shopCustomerDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<UserManagerVO> shopCustomerPageVO = userService.pageShopCustomerByParam(pageDTO, shopCustomerDTO);
        return ServerResponseEntity.success(shopCustomerPageVO);
    }



    /**
     * 修改
     */
    @PutMapping
    public ServerResponseEntity<Void> update(@RequestBody UserDTO userDTO) {
        userService.updateUserInfo(userDTO);
        return ServerResponseEntity.success();
    }

    @GetMapping("/user_info")
    @Operation(summary = "根据用户id获取会员信息", description = "根据用户id获取会员信息")
    public ServerResponseEntity<UserManagerVO> info(@RequestParam Long userId) {
        UserManagerVO param = userService.getUserInfo(userId);
        List<UserTagApiVO> userTagParams = userTagUserService.getUserTagsByUserId(userId);
        param.setUserTagList(userTagParams);
        return ServerResponseEntity.success(param);
    }

    @GetMapping("/sold_excel")
    @Operation(summary = "导出excel", description = "导出客户信息excel")
    public ServerResponseEntity<Void> userSoldExcel(HttpServletResponse response, @ParameterObject UserManagerDTO userManagerDTO){
        userExcelManager.exportUserInfoExcel(response, userManagerDTO);
        return ServerResponseEntity.success();
    }

    @GetMapping("/down_model")
    @Operation(summary = "下载客户信息excel模板", description = "下载客户信息excel模板")
    public ServerResponseEntity<Void> downloadModel(HttpServletResponse response) {
        userExcelManager.downloadShopCustomerModel(response);
        return ServerResponseEntity.success();
    }

    @PostMapping("/import_excel")
    @Operation(summary = "导入excel", description = "导入客户信息excel")
    public ServerResponseEntity<String> importExcel(@RequestParam("excelFile") MultipartFile file){
        if(file == null) {
            throw new LuckException("网络繁忙，请稍后重试");
        }
        String info = userExcelManager.importShopCustomerExcel(file);
        return ServerResponseEntity.success(info);
    }


}
